//
//  JFCheckBoxControl.m
//  SLAppProduct
//
//  Created by AlanWang on 2018/3/15.
//  Copyright © 2018年 Sunline. All rights reserved.
//

#import "JFCheckBoxControl.h"
#import "JFKitPri.h"
@interface JFCheckBoxControl()
@property(nonatomic,strong) UIImageView * imageIco;
@property(nonatomic,strong) UILabel * title;
@property(nonatomic,strong) UILabel * line;
@end
@implementation JFCheckBoxControl

-(instancetype)init{
    self = [super init];
    if (self) {
        [self initView];
        [self addTap];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
        [self addTap];

    }
    return self;
}
-(void)addTap{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTapChange)];
    tap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tap];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat imageIcoWidth = 15;
    CGFloat margin = 5;
    CGFloat icoY = (self.width -imageIcoWidth) / 2;
    CGFloat titleWidth = self.width - margin -imageIcoWidth- self.iconMarginLeft;
    self.imageIco.frame = CGRectMake(self.iconMarginLeft, icoY, imageIcoWidth, imageIcoWidth);
    if (self.isNeedLine) {
        self.title.frame = CGRectMake(CGRectGetMaxX(self.imageIco.frame)+ margin, 0, titleWidth, self.height-1);
        self.line.frame = CGRectMake(CGRectGetMaxX(self.imageIco.frame), self.height - 1, self.width-CGRectGetMaxX(self.imageIco.frame), 1);
    }else{
        self.title.frame = CGRectMake(CGRectGetMaxX(self.imageIco.frame)+ margin, 0, titleWidth, self.height);
    }
    self.selected = self.isSelected;
}

-(void)initView{
    self.iconMarginLeft = 0;
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.imageIco];
    [self addSubview:self.title];
    [self addSubview:self.line];
}
-(void)setSelected:(BOOL)selected{
    _selected = selected;
    if (_selected) {
        [self selectStyle];
    }else{
        [self unSelectStyle];
    }
}
-(void)setTitleStr:(NSString *)titleStr{
    _titleStr = titleStr;
    self.title.text = _titleStr;
}
-(void)doTapChange{
    if (!self.unEnable) {
        self.selected = !self.isSelected;
        if (self.block && !self.unEnable) {
            self.block(self.isSelected, self.tag,self);
        }
    }
}
-(void)selectStyle{
    if (self.selectedImage) {
        self.imageIco.image = self.selectedImage;
    }
    if (self.selectedTitleColor) {
        self.title.textColor = self.selectedTitleColor;
    }
    if(self.selectedBackgroundColor){
        self.backgroundColor = self.selectedBackgroundColor;
    }
}

-(void)unSelectStyle{
    if (self.unSelectedImage) {
        self.imageIco.image = self.unSelectedImage;
    }
    if (self.unSelectedTitleColor) {
        self.title.textColor = self.unSelectedTitleColor;
    }
    if (self.unSelectedBackgroundColor) {
        self.backgroundColor = self.unSelectedBackgroundColor;
    }
}
#pragma mark--懒加载
-(UIImageView*)imageIco{
    if (!_imageIco) {
        _imageIco = [[UIImageView alloc] init];
        _imageIco.backgroundColor = [UIColor clearColor];
    }
    return _imageIco;
}

-(UILabel*)title{
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.backgroundColor = [UIColor clearColor];
    }
    return _title;
}
-(UILabel*)line{
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.hidden = YES;
    }
    return _line;
}

-(void)setIconMarginLeft:(CGFloat)iconMarginLeft{
    _iconMarginLeft = iconMarginLeft;
    [self setNeedsLayout];
    [self setNeedsDisplay];
}
-(void)setNeedLine:(BOOL)needLine{
    _needLine = needLine;
    self.line.hidden = !needLine;
    [self setNeedsLayout];
    [self setNeedsDisplay];
}

-(void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
    self.line.backgroundColor = lineColor;
}
-(void)setTxtFont:(UIFont *)txtFont{
    _txtFont = txtFont;
    if (txtFont) {
      [self.title setFont:_txtFont];
    }
}
@end
